package com.zbowen.way.utils;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.*;

public class ResultUtils {


    public static final Map<String, String> restrictionMap = new HashMap<String, String>(){{
        put("0", "限行已规避或未限行");
        put("1", "限行无法规避");
    }};

    public static void print(String json) {
        JSONObject result = JSONObject.parseObject(json);
        String count = result.getString("count");
        JSONObject route = result.getJSONObject("route");
        // 预计出租车费用，单位：元
        String taxiCost = route.getString("taxi_cost");

        System.out.println("规划出" + count + "条驾驶路线");
        System.out.println("预计出租车费用为" + taxiCost + "元");

        JSONArray paths = route.getJSONArray("paths");
        for (int i = 0; i < paths.size(); i++) {
            JSONObject way = paths.getJSONObject(i);
            System.out.println("\t路线" + (i+1) + "：");
            // 总里程 单位：米
            int distance = way.getInteger("distance");
            // 限行信息
            String restriction = way.getString("restriction");
            JSONObject cost = way.getJSONObject("cost");
            // 总耗时 单位：秒
            int duration = cost.getInteger("duration");
            // 收费信息 单位：元
            String tolls = cost.getString("tolls");
            // 红绿灯个数 单位：个
            String trafficLights = cost.getString("traffic_lights");

            System.out.println("\t\t预计耗时：" + (duration / 60) + " 分钟");
            System.out.println("\t\t总里程：" + (distance * 1.0 / 1000.0) + " 公里");
            System.out.println("\t\t限行信息：" + restrictionMap.get(restriction));
            System.out.println("\t\t收费信息：" + tolls + " 元");
            System.out.println("\t\t红绿灯信息：" + trafficLights + " 个");

        }
    }

    public static List<Map<String, Object>> print2(String json, String place) {

        List<Map<String, Object>> list = new ArrayList<>();

        JSONObject result = JSONObject.parseObject(json);
        String count = result.getString("count");
        JSONObject route = result.getJSONObject("route");
        // 预计出租车费用，单位：元
        String taxiCost = route.getString("taxi_cost");

        System.out.println("预计打车费用为" + taxiCost + "元");
        System.out.println("规划出" + count + "条驾驶路线");

        JSONArray paths = route.getJSONArray("paths");
        for (int i = 0; i < paths.size(); i++) {

            Map<String, Object> map = new HashMap<>();
            list.add(map);

            JSONObject way = paths.getJSONObject(i);
            System.out.println("\t" + place + "-路线" +  (i+1) + "：");

            map.put("name", place + "-路线" + (i+1));

            // 总里程 单位：米
            int distance = way.getInteger("distance");
            // 限行信息
            String restriction = way.getString("restriction");
            // 总耗时 单位：秒
            int duration = way.getInteger("duration");
            // 收费信息 单位：元
            String tolls = way.getString("tolls");
            // 红绿灯个数 单位：个
            String trafficLights = way.getString("traffic_lights");
            // 驾车选择策略
            String strategy = way.getString("strategy");

            map.put("distance", distance);
            map.put("duration", duration);
            map.put("restriction", restriction);
            map.put("tolls", tolls);
            map.put("lights", trafficLights);
            map.put("strategy", strategy);

            System.out.println("\t\t预计耗时：" + (duration / 60) + " 分钟");
            System.out.println("\t\t总里程：" + (distance * 1.0 / 1000.0) + " 公里");
            System.out.println("\t\t限行信息：" + restrictionMap.get(restriction));
            System.out.println("\t\t收费信息：" + tolls + " 元");
            System.out.println("\t\t红绿灯信息：" + trafficLights + " 个");
            System.out.println("\t\t驾车选择策略：" + strategy);

            JSONArray steps = way.getJSONArray("steps");

            String preRoad = null;
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < steps.size(); j++) {
                JSONObject step = steps.getJSONObject(j);
                String road = step.getString("road");
                if ("宾隆路".equals(road) || "新湖路".equals(road)) {
                    continue;
                }
                if (road != null && !road.equals(preRoad)) {
                    sb.append(road + " --> ");
                    preRoad = road;
                }
            }
            System.out.println("\t\t" + sb.substring(0, sb.length() - 5));
            map.put("road", sb.substring(0, sb.length() - 5));
        }
        return list;
    }

}
